﻿Imports System.Threading
Imports System.Net.Sockets
Imports System.Text
Imports System.IO

Public Class MTPTransfer
    Private input As String
    Private numConnectoins As Integer
    Private serverIP As String
    Private portNum As Integer
    Private output As String
    Private uID As Integer
    Private threads As ArrayList = New ArrayList
    Private size, partsize As Long
    Property finished = False

    Public Sub New(ByVal filename As String, ByVal connections As Integer, ByVal server As String, ByVal port As Integer, ByVal outputPath As String)
        input = filename
        numConnectoins = connections
        serverIP = server
        portNum = port
        output = outputPath
    End Sub

    Public Sub start()
        Dim t As New Thread(AddressOf transferTask)
        t.Start()
    End Sub

    Private Sub transferTask()
        Dim first As New TcpClient
        first.Connect(serverIP, portNum)
        Dim message(260) As Byte
        message(0) = 0
        Encoding.UTF8.GetBytes(input, 0, input.Length, message, 1)
        Dim num As Byte() = BitConverter.GetBytes(numConnectoins)
        Dim i As Integer = 1
        While (i <= num.Length)
            message(message.Length - i) = num(num.Length - i)
            i += 1
        End While
        Dim writer As New NetworkStream(first.Client)
        Dim reader As New BinaryReader(writer)
        writer.Write(message, 0, message.Length)
        writer.Flush()
        uID = reader.ReadInt32()
        size = reader.ReadInt64()
        partsize = reader.ReadInt64()
        first.Close()
        writer.Close()
        reader.Close()
        Dim createFile As New FileStream(output, FileMode.Create)
        Dim tmparray(100000000) As Byte
        Dim tmpsz As Long = size
        While (tmpsz - 100000000 > 0)
            createFile.Write(tmparray, 0, 100000000)
            tmpsz -= 100000000
        End While
        createFile.Write(tmparray, 0, tmpsz)
        'Dim tmparray(size) As Byte
        'createFile.Write(tmparray, 0, size)
        createFile.Close()
        Dim thrind As Integer = 0
        While (thrind < numConnectoins)
            Dim t As New Thread(AddressOf transferSubTask)
            t.Start(thrind)
            threads.Add(t)
            thrind += 1
        End While
        Dim tmp As Integer
        While (Not finished)
            tmp = 0
            For Each t As Thread In threads
                tmp += 0
                If (Not t.IsAlive) Then
                    tmp += 1
                    Debug.WriteLine(tmp)
                End If
            Next
            If (tmp = threads.Count) Then
                finished = True
            End If
        End While
    End Sub

    Private Sub transferSubTask(ByVal num As Integer)
        Dim tcp As New TcpClient
        tcp.Connect(serverIP, portNum)
        Dim message(8) As Byte
        Dim numOne() As Byte = BitConverter.GetBytes(num)
        Dim numTwo() As Byte = BitConverter.GetBytes(uID)
        Dim i As Integer = 1
        While (i <= numOne.Length)
            message(message.Length - i) = numOne(numOne.Length - i)
            i += 1
        End While
        Dim b As Integer = 1
        While (i <= numTwo.Length)
            message(message.Length - i) = numTwo(numTwo.Length - b)
            i += 1
        End While
        message(0) = 99
        Dim writer As New NetworkStream(tcp.Client)
        writer.Write(message, 0, message.Length)
        writer.Flush()
        Dim mybuffer(25000000) As Byte
        Dim writeFile As New FileStream(output, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
        writeFile.Position = partsize * num
        Dim read As Integer = writer.Read(mybuffer, 0, 25000000)
        Dim count As Integer = 0
        While read > 0
            writeFile.Write(mybuffer, 0, read)
            writeFile.Flush()
            read = writer.Read(mybuffer, 0, 25000000)
        End While
        writeFile.Close()
        writer.Close()
    End Sub

End Class
